/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        // 点击排序
        if(sorter.order && sorter.field) {
            option.sortField = sorter.field;
            if(sorter.order === 'descend')
                option.isDesc = true;
            else
                option.isDesc = false;
        } else { // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};

/**
 *  原始数据转化为支持单元格行合并的数据
 *  使用场景 清单映射清单时，主单字段单元格合并
 *  @data 原表格数据
 *  @groupByKey 合并数据的key (主单数据的Id)
 *  @rowSpanKeyList 需要合并的字段 (编号、名称、操作列等)
 */
import {groupBy} from 'lodash';
export const generateRowSpanDatas = (details, groupByKey, rowSpanKeyList) => {
    // const details = flattenData(data);
    const groupByData = groupBy(details, groupByKey);
    const result = [];
    for(const key in groupByData) {
        const array = groupByData[key];
        array.forEach((item, index) => {
            if(index === 0) {
                item.rowSpan = array.length;
                result.push(item);
            } else {
                rowSpanKeyList.forEach(rowSpanKey => {
                    delete item[rowSpanKey];
                });
                result.push(item);
            }
        });
    }
    return result;
};
/**
 *  原始数据转化为支持单元格行合并的数据
 *  使用场景 清单映射清单时，主单字段单元格合并
 *  @data 原表格数据
 *  @groupByKey 合并数据的key (主单数据的Id)
 *  @rowSpanKeyList 需要合并的字段 (编号、名称、操作列等)
 */
export const generateRowSpanData = (details, groupByKey) => {
    // const details = flattenData(data);
    const groupByData = groupBy(details, groupByKey);
    const result = [];
    for(const key in groupByData) {
        const array = groupByData[key];
        // eslint-disable-next-line no-loop-func
        array.forEach(item => {
            let threeVINWeight = 0;
            array.forEach(v => {
                threeVINWeight += v.perGW;
            });
            item.threeVINWeight = threeVINWeight;
            result.push(item);
        });
    }
    return result;
};

export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);

import qs from 'qs';
export const getUrl = str => {
    const r = qs.parse(str.replace('?', ''));
    if(r !== null)
        return r;
    return null;
};
